package com.ldc.wandroid.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager2.widget.ViewPager2;

import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.ProjectTabAdapter;
import com.ldc.wandroid.adapter.ProjectTabTitleAdapter;
import com.ldc.wandroid.databinding.FragmentProjectTabBinding;
import com.ldc.wandroid.model.ProjectsModel;
import com.ldc.wandroid.viewmodel.ProjectTabViewModel;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ProjectTabFragment extends BaseMvvmFragment<FragmentProjectTabBinding, ProjectTabViewModel> {
    private ProjectTabAdapter projectTabAdapter;
    private ProjectTabTitleAdapter tabTitleAdapter;
    private final ViewPager2.OnPageChangeCallback pageChangeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            viewBinding.rvTitle.scrollToPosition(position);
            tabTitleAdapter.setSelectPosition(position);
        }
    };


    public static ProjectTabFragment newInstance() {
        Bundle args = new Bundle();
        ProjectTabFragment fragment = new ProjectTabFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getProjects().removeObserver(this::processProjects);
        viewBinding.viewPage2.unregisterOnPageChangeCallback(pageChangeCallback);
    }

    @Override
    protected FragmentProjectTabBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentProjectTabBinding.inflate(inflater, container, false);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        initAdapter();
        // 加载数据
        viewModel.getProjects().observeForever(this::processProjects);
        viewModel.loadProjects();


    }

    @Override
    protected ProjectTabViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, ProjectTabViewModel.class);
    }


    private void initAdapter() {

        projectTabAdapter = new ProjectTabAdapter(getChildFragmentManager(), getLifecycle());
        tabTitleAdapter = new ProjectTabTitleAdapter();
        // title
        viewBinding.rvTitle.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
        viewBinding.rvTitle.setAdapter(tabTitleAdapter);
        tabTitleAdapter.addRowListener(data -> viewBinding.viewPage2.setCurrentItem(data));
        //
        viewBinding.viewPage2.setCurrentItem(0);
        viewBinding.viewPage2.registerOnPageChangeCallback(pageChangeCallback);
        viewBinding.viewPage2.setAdapter(projectTabAdapter);
    }


    private void processProjects(List<ProjectsModel> models) {
        viewBinding.stateView.showContentView();
        final List<Fragment> fragments = new ArrayList<>();
        final List<String> titles = new ArrayList<>();
        for (ProjectsModel item : models) {
            fragments.add(ProjectInfoFragment.newInstance(item.getName(), "" + item.getId()));
            titles.add(item.getName());
        }
        projectTabAdapter.addFragments(fragments);
        tabTitleAdapter.setNewData(titles);

    }

}
